package tec;

import java.io.File;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;

public class CollecteFichier implements Collecte {
	int nbEntrees, nbSorties, arret;
	File file;
	FileWriter writeOut;

	public CollecteFichier() {
		this("fichier de sauvegarde avec des espaces (et des parentheses)");
	}

	public CollecteFichier(String fileName) {
		nbEntrees = 0;
		nbSorties = 0;
		arret = 0;
		file = new File(fileName);
		try {
			writeOut = new FileWriter(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void uneEntree() {
		nbEntrees++;
	}
	public void uneSortie() {
		nbSorties++;
	}
	public void changerArret() {
		try {
			writeOut.write("Arret : " + Integer.toString(arret) + ", Entrees: " + Integer.toString(nbEntrees)
			 + ", Sorties:" + Integer.toString(nbSorties) + "\n");
			 writeOut.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		nbEntrees=0;
		nbSorties=0;
		arret++;
	}

	public void printCollected() {
		try{
			FileReader rdr = new FileReader(file);
			int c;
			while((c = rdr.read()) != -1)
				System.out.print((char) c);
		} catch (IOException e) { e.printStackTrace(); }
	}
	
	public void finalize() {
		try{
			writeOut.close();
		} catch (IOException e) { e.printStackTrace(); }
	}
}
